/* Copyright (c) 2010, Michael J. Allison

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are met:

 * Redistributions of source code must retain the above copyright notice, 
 this list of conditions and the following disclaimer.
 
 * Redistributions in binary form must reproduce the above copyright notice, 
 this list of conditions and the following disclaimer in the documentation 
 and/or other materials provided with the distribution.
 
 * Neither the name of Michael J. Allison nor the names of contributors
   may be used to endorse or promote products derived from this software
   without specific prior written permission.
 
 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
 THE POSSIBILITY OF SUCH DAMAGE.
 */

package xamcommons.objectinspector;

import java.awt.Frame;
import java.awt.Rectangle;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class PlainCredentialsDialog
   extends JDialog
   implements ActionListener
{
   private final String CMD_OK     = "$OK$";
   private final String CMD_CANCEL = "$Cancel$";
   
   private JTextField m_username_field;
   private JTextField m_password_field;
   
   private String     m_username;
   private String     m_password;
   
   private boolean m_is_ok = false;
   
   public PlainCredentialsDialog( Frame owner, String last_username )
   {
      super( owner, true );
      setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
      Box vbox = Box.createVerticalBox();
      
      Box hbox = Box.createHorizontalBox();
      JLabel label = new JLabel( "Username: " );
      m_username_field = new JTextField(last_username);
      hbox.add( Box.createHorizontalStrut( 10 ) );
      hbox.add( label );
      hbox.add( m_username_field );
      hbox.add( Box.createHorizontalStrut( 10 ) );
      vbox.add( hbox );
      
      label = new JLabel( "Password: " );
      m_password_field = new JTextField();
      hbox = Box.createHorizontalBox();
      hbox.add( Box.createHorizontalStrut( 10 ) );
      hbox.add( label );
      hbox.add( m_password_field );
      hbox.add( Box.createHorizontalStrut( 10 ) );
      vbox.add( hbox );
      
      // OK/Cancel
      Box controls = Box.createHorizontalBox();
      JButton ok_button = new JButton( "OK" );
      ok_button.setActionCommand( CMD_OK );
      ok_button.addActionListener( this );
      controls.add( ok_button );
      
      JButton cancel_button = new JButton( "Cancel" );
      cancel_button.setActionCommand( CMD_CANCEL );
      cancel_button.addActionListener( this );
      controls.add( cancel_button );
      
      vbox.add( controls );
      this.add( vbox );
      this.setSize( 320, 240 );
      this.setTitle( "Credentials" );
      this.pack();
      
      // Center on parent
      if( owner != null )
      {
         Rectangle r = owner.getBounds();
         Rectangle d = getBounds();
         int x = (int) ((r.getWidth() - d.getWidth()) / 2 + r.getX());
         int y = (int) ((r.getHeight() - d.getHeight()) / 2 + r.getY());
         setLocation( x, y );
      }
      
      this.setVisible( true );
   }
   
   /**
    * Return true if the user dismissed the dialog with the OK button, false otherwise.
    */
   public boolean isOK() { return m_is_ok; }
   
   /**
    * Return the user entered value of the username. 
    */
   public String getUsername() { return m_username; }
   
   /**
    * Return the user entered value of the password.
    */
   public String getPassword() { return m_password; }
   
   /**
    * Respond to dialog actions; close, edit XRI, etc. 
    */
   public void actionPerformed( ActionEvent e )
   {
      String command = e.getActionCommand();
      
      if( command.equals( CMD_OK ) )
      {
         m_is_ok = true;
         m_username = m_username_field.getText();
         m_password = m_password_field.getText();
         setVisible( false );
      }
      
      if( command.equals( CMD_CANCEL ) )
      {
         setVisible( false );
      }
   }
}